/**
 * @author spider
 * Implementation of socket for the status (see Socket on core.message)
 */
package br.unb.core.status;

import br.unb.core.message.Message;
import br.unb.core.synchronism.Synchronism;
import br.unb.graphics.GraphicInterface;
import br.unb.utils.Console;
import br.unb.utils.ConsoleInterface;
import br.unb.utils.Resource;
import javax.swing.text.DefaultStyledDocument;

/**
 *
 * @author spider
 */
public class Status implements Runnable, ConsoleInterface {

    static final StatusSocket STATUS_SOCKET = StatusSocket.getInstance();

    /*Instance*/
    private static final Status INSTANCE = new Status();
    private Thread statusThread = null;
    private boolean isRunning = false;

    private Status() {
        isRunning = true;
        statusThread = (new Thread(this, "Status"));
        statusThread.start();
    }

    public static Status getInstance() {
        return INSTANCE;
    }

    @Override
    public void run() {
        while (isRunning) {
            Message reply = STATUS_SOCKET.wait4Status(); //TODO: Im not done yet!!
            println(reply.toString());
            switch (reply.getType()) {
                /**
                 * Message.ERROR and Message.ACK are expected to be received by
                 * the Manager as an answer to control messages not by the dispatcher!
                 * - They are not meaningful to the Dispatcher and will be ignored silently.
                 * - They are meant to be received inside a manager method like TrafficManager.playAll().
                 */
                case ACK: {
                    println(
                            Resource.getString("ACK")
                            + reply.getText());
                }
                break;
                case ERROR: {
                    printError(
                            Resource.getString("ERROR")
                            + reply.getText());
                }
                break;
                case OK_SYNC: {
                    //* Received on Controller machine
                    okSync((Synchronism) reply.getSyncModel());
                }
                break;
                case DIALOG: {
                    dialog(reply.getText());
                }
                break;
                default: {
                    printError(Integer.toString(reply.getType().ordinal()));
                    printError(Resource.getString("UNKNOW"));
                }
                break;
            }
        }
    }

    /************************* Extras ****************************/
    /**
     * (Executed on Manager (controller machine))
     * After a message is received do the apropriate actions
     * okSync, dialog
     */
    public Synchronism okSync(Synchronism syncModel) {
        println("SYNCHRONIZED");
        GraphicInterface.getInstance().getSyncPanel().confirm(syncModel);
        return syncModel;
    }

    public String dialog(String s) {
        GraphicInterface.getChatPanel().addMessage(s);
        return s;
    }
    /** ConsoleInterface methods */
    private static DefaultStyledDocument doc = new DefaultStyledDocument();

    @Override
    public void printError(String s) {
        Console.println(s, System.err, doc);
    }

    @Override
    public void println(String s) {
        Console.println(s, System.out, doc);
    }

    @Override
    public void setDocument(DefaultStyledDocument doc) {
        Status.doc = doc;
    }
}
